package com.xmy.cultivate.schedule;

import com.xmy.cultivate.service.ITranscriptWritePushSetService;
import com.xmy.cultivate.util.SpringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

@Component
public class MyJob implements Job {
    @Autowired
    ITranscriptWritePushSetService iTranscriptWritePushSetService = SpringUtils.getBean(ITranscriptWritePushSetService.class);
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private void before() {
        System.err.println("任务TranscriptJob：开始执行-" + dateFormat.format(new Date()));
    }
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        before();

        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        // 获取JobDataMap中的数据
        String id = dataMap.getString("id");
        //获取推送时间
        String pushTime = dataMap.getString("pushTime");
        //获取当前时间年月日
        LocalDate pushDate = LocalDate.now();
        // 这里编写推送逻辑
        try {
            iTranscriptWritePushSetService.scoreScheduledPush(id, pushDate,pushTime);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        after();
    }

    private void after() {
        System.err.println("任务TranscriptJob：执行结束");
        System.err.println();
    }
}